package com.probiz.estore.catalog.dao.impl;

import com.probiz.estore.catalog.dao.ProductCategoryDao;
import com.probiz.estore.catalog.model.ProductCategorySearchCriteria;
import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.common.util.SearchUtil;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.search.SearchCriteria;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Dao implementation for ProductCategory.
*/
public class ProductCategoryDaoImpl extends BaseCategoryDaoImpl<ProductCategory> implements ProductCategoryDao {
    
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.ProductCategoryDao#getCategoryByCode(java.lang.String)
	 */
	public ProductCategory getCategoryByCode(String categoryCode) {
		return (ProductCategory) this.findUnique("from ProductCategory c where c.categoryCode=?", categoryCode );
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.ProductCategoryDao#listCategoryByParentIdAndName(java.lang.Integer, java.lang.String)
	 */
	public List<ProductCategory> listCategoryByParentIdAndName(Integer parentCategoryId, String categoryName){
		String hql = "from ProductCategory c where c.parent.categoryId=? and c.categoryName=?  order by c.sortOrder, c.categoryId";
		List<ProductCategory> categoryList = findByHql(hql, new Object[] { parentCategoryId, categoryName });
		return categoryList;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.ProductCategoryDao#searchByCriteria(com.probiz.estore.catalog.model.ProductCategorySearchCriteria, com.probiz.estore.core.search.SearchCriteria)
	 */
	public List<ProductCategory> searchByCriteria(ProductCategorySearchCriteria pcSearchCriteria, SearchCriteria searchCriteria){
		StringBuffer hqlsb = new  StringBuffer(100);
		List<Object> paramList=new ArrayList<Object>();
		//start:构建HQL语句
		hqlsb.append("from ProductCategory pc where 1=1 ");
		if(StringUtils.isNotBlank(pcSearchCriteria.getKeywords())){
			String keywords = SearchUtil.getSqlQueryValue(pcSearchCriteria.getKeywords());
			hqlsb.append(" and ");
			hqlsb.append("(pc.categoryName like ? ");
			hqlsb.append(" or pc.keywords like ? ");
			hqlsb.append(" or pc.metaDescription like ? )");
			for(int i=0;i<3;i++){
				paramList.add("%"+keywords+"%");
			}
		}

		if(StringUtils.isNotBlank(pcSearchCriteria.getCategoryName())){
			hqlsb.append(" and pc.categoryName like ?");
			paramList.add("%"+pcSearchCriteria.getCategoryName()+"%");
		}

		if(null != pcSearchCriteria.getStatus()){
			hqlsb.append(" and pc.status = ?");
			paramList.add(pcSearchCriteria.getStatus());
		}
		//end:构建HQL语句
		searchCriteria.setHql(hqlsb.toString());
		searchCriteria.removeParamValues();
		for (Object object : paramList) {

			searchCriteria.addParamValue(object);
		}
		return  searchByCriteria(searchCriteria);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.ProductCategoryDao#getNavigationCategories(java.lang.Integer)
	 */
	public List<ProductCategory> getNavigationCategories(Integer categoryId) {
		List<ProductCategory> navigationCategories = new ArrayList<ProductCategory>();
		ProductCategory productCategory = this.getById(categoryId);
		if (productCategory == null)
			return navigationCategories;
		String categoryPath = productCategory.getCategoryPath();
		if (StringUtils.isNotEmpty(categoryPath)) {
			String categoryIds[] = categoryPath.split("\\.");
			for (int i = 1; i < categoryIds.length; i++) {
				ProductCategory parentProductCategory = this
						.getById(Integer.parseInt(categoryIds[i]));
				navigationCategories.add(parentProductCategory);
			}
		}
		return navigationCategories;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.ProductCategoryDao#getSizeByCategoryCodeByNotCategoryId(java.lang.String, java.lang.Integer)
	 */
	public Long getSizeByCategoryCodeByNotCategoryId(final String categoryCode,final Integer categoryId){
		Long size;
    	if(categoryId != null){
    		
    		size = (Long)getHibernateTemplate().executeWithNewSession(new HibernateCallback() {
    			public Object doInHibernate(Session session)
    					throws HibernateException, SQLException {
    				StringBuffer sb = new StringBuffer();
    				sb.append("select count(*) from ProductCategory pc where pc.categoryCode = ? and pc.categoryId != ?");
    				Query query = session.createQuery(sb.toString());
    				query.setString(0, categoryCode);
    				query.setInteger(1, categoryId);
    				Long rows =  (Long)query.uniqueResult();
    				return rows;
    			}

    		});
    	}else{
    		size = (Long)getHibernateTemplate().executeWithNewSession(new HibernateCallback() {
    			public Object doInHibernate(Session session)
    					throws HibernateException, SQLException {
    				StringBuffer sb = new StringBuffer();
    				sb.append("select count(*) from ProductCategory pc where pc.categoryCode = ?");
    				Query query = session.createQuery(sb.toString());
    				query.setString(0, categoryCode);
    				Long rows =  (Long)query.uniqueResult();
    				return rows;
    			}

    		});
    	}	
    	return size;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.dao.ProductCategoryDao#getAllProductCategoryCodes()
	 */
	public List<String> getAllProductCategoryCodes(){
		List<String> categoryCodeList = findByHql("select c.categoryCode from ProductCategory c where c.categoryId!=1  c.status!=-1");
		return categoryCodeList;
	}
	
	public List<ProductCategory> getCategorysByCategoryPath(String categoryPath){
		String hql = " from ProductCategory c where c.categoryPath like ? order by c.categoryPath desc";
		List<ProductCategory> productCategoryList = this.findByHql(hql, new Object[]{categoryPath + "%"});
		return productCategoryList;
	}
	
	public List<ProductCategory> getCategorysByNameAndParentId(String name, Integer parentId){
		String hql = " from ProductCategory c where c.categoryName = ? and c.parent.categoryId = ? ";
		List<ProductCategory> productCategoryList = this.findByHql(hql, new Object[]{name, parentId});
		return productCategoryList;
	}
}
